var path = "";
function getPath(pathVal) {
	path = pathVal;
}
var requiredArray = [ "#loginCode", "#passwd", "#repeatPwd", "#orgCode", "#userName",
                      "#tel", "#mobile", "#postCode", "#captcha" ];
var notRequiredArray = [ "#djjg", "#fax", "#unitWebsite" ];
var idArray = requiredArray.concat(notRequiredArray);

//必填项提示信息
var LOGIN_CODE_MSG = "请输入6~18个字符，可使用字母、数字、下划线，需以字母开头";
var PASSWD_MSG = "请输入6~16个字符，区分大小写";
var REPEAT_PASSWD_MSG = "请再次填写密码";
var ORG_CODE_MSG = "请根据成立批文填写拟登记单位的第一名称";
var USER_NAME_MSG = "请输入联系人姓名";
var TEL_MSG = "请输入联系人办公电话";
var MOBILE_MSG = "请输入联系人移动电话";
var POST_CODE_MSG = "请输入单位邮政编码";
var CAPTCHA_MSG = "请输入验证码";

//不是必填项的提示信息
var OWNERID_MSG = "请选择单位所属的登记机关";
var FAX_MSG = "请输入单位传真号";
var UNIT_WEBSITE_MSG = "请填写单位的网站网址";

//格式不正确或错误时的提示信息
var EXIST_LOGIN_CODE_MSG = "用户名已存在";
var PASSWD_LIKE_LOGIN_CODE = "密码不能与用户名一样";
var REPEAT_PASSWD_ERROR_MSG = "两次输入的密码不一致";
var ORG_CODE_ERROR_MSG = "单位名称不能超过250个字";
var EXIST_ORG_CODE_MSG = "单位名称已存在";
var POST_CODE_ERROR_MSG = "邮政编码格式不正确";
var UNIT_WEBSITE_ERROR_MSG = "单位的网站网址不能为数字";
var MOBILE_ERROR_MSG = "移动电话格式不正确";
var CAPTCHA_ERROR_MSG = "验证码不正确或验证码已过期，请重新输入";
var ERROR_MSG = "网络有误 ！请稍后重试";

var requiredInfoArray = [ LOGIN_CODE_MSG, PASSWD_MSG, REPEAT_PASSWD_MSG, ORG_CODE_MSG, USER_NAME_MSG, TEL_MSG,
                          MOBILE_MSG, POST_CODE_MSG, CAPTCHA_MSG, OWNERID_MSG, FAX_MSG, UNIT_WEBSITE_MSG ];

function reload(path, id) {
	document.getElementById(id).src = path + "/image.jsp?imageId=" + id + "&r=" + Math.random();
}

function noShowMsg(childId) {
	$(childId).removeClass("blueInput").removeClass("redInput");
	return $(childId).parent().next().html("");
}

function showCueMsg(childId, msg) {
	$(childId).addClass("blueInput");
	return $(childId).parent().next().html("<div class='cueImage'></div><div class='cueMsg'>" + msg + "</div>");
}

function showErrorMsg(childId, msg) {
	$(childId).removeClass("blueInput").addClass("redInput");
	return $(childId).parent().next().html("<div class='errorImage'></div><div class='errorMsg'>" + msg + "</div>");
}

function showSuccessImg(childId) {
	$(childId).removeClass("blueInput").removeClass("redInput");
	return $(childId).parent().next().html("<div class='successImg'></div><div class='cueMsg'></div>");
}

function mouseoverAndOutClass() {
	$("#gotoRegister").mouseover(function() {
		$(this).removeClass("register1").addClass("register2");
	}).mouseout(function() {
		$(this).removeClass("register2").addClass("register1");
	});
}

/**
 * 验证是否为空
 * @param id
 * @returns true 不为空，false 为空
 */
function validIsNotNull(id) {
	var isFlag = true;
	if ($(id).val() == null || $(id).val() == undefined || $.trim($(id).val()) == "") {
		isFlag = false;
	}
	return isFlag;
}

function validIsNaN(id) {
	var isFlag = true;
	if (isNaN($.trim($(id).val()))) {
		isFlag = false;
	}
	return isFlag;
}

function validNumLength(id, length) {
	var isFlag = true;
	if ($.trim($(id).val()).length < length) {
		isFlag = false;
	}
	return isFlag;
}

function ajaxValidLoginCode(url, id) {
	var isFlag = false;
	$.ajax({
		url : url,
		type : 'post',
		cache : false,
		async : false,
		data : {loginCode : $(id).val()},
		success : function(data) {
			isFlag = data.successful;
		},
		error : function() {
			showErrorMsg(id, ERROR_MSG);
			isFlag = true;
		}
	});
	return isFlag;
}

function ajaxValidOrgCode(url, id) {
	var isFlag = false;
	$.ajax({
		url : url,
		type : 'post',
		cache : false,
		async : false,
		data : {orgCode : $(id).val()},
		success : function(data) {
			isFlag = data.successful;
		},
		error : function() {
			showErrorMsg(id, ERROR_MSG);
			isFlag = true;
		}
	});
	return isFlag;
}

function ajaxValidCaptcha(url, id) {
	var isFlag = true;
	$.ajax({
		url : url,
		type : 'post',
		cache : false,
		async : false,
		data : {
			captchaImgId : "validCode",
			captCha : $(id).val()
		},
		success : function(data) {
			isFlag = data.successful;
		},
		error : function() {
			showErrorMsg(id, ERROR_MSG);
			isFlag = false;
		}
	});
	return isFlag;
}

//验证用户名
function validLoginCode(id) {
	var isFlag = true;
	var re = /^[a-zA-Z].{5,17}$/;
	if (!validIsNotNull(id) || !validNumLength(id, 6) || !re.test($(id).val())) {
		showErrorMsg(id, LOGIN_CODE_MSG);
		isFlag = false;
	} else if (ajaxValidLoginCode(path + "/isExistLoginCode.do", id)) {
		showErrorMsg(id,EXIST_LOGIN_CODE_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证密码
function validPassword(id,loginCodeId) {
	var isFlag = true;
	if (!validIsNotNull(id) || !validNumLength(id, 6)) {
		showErrorMsg(id, PASSWD_MSG);
		isFlag = false;
	} else if ($.trim($(id).val()) == $.trim($(loginCodeId).val())) {
		showErrorMsg(id, PASSWD_LIKE_LOGIN_CODE);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证确认密码
function validRepeatPwd(id, repeatId) {
	var isFlag = true;
	if (!validIsNotNull(repeatId) || !validNumLength(repeatId, 6)) {
		showErrorMsg(repeatId, REPEAT_PASSWD_MSG);
		isFlag = false;
	} else {
		if ($.trim($(repeatId).val()) != $.trim($(id).val())) {
			showErrorMsg(repeatId, REPEAT_PASSWD_ERROR_MSG);
			isFlag = false;
		} else {
			showSuccessImg(repeatId);
			isFlag = true;
		}
	}
	return isFlag;
}

//验证登记机关
function validDjjg(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, OWNERID_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证单位名称
function validOrgCode(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, ORG_CODE_MSG);
		isFlag = false;
	} else if ($(id).val().length > 250) {
		showErrorMsg(id, ORG_CODE_ERROR_MSG);
		isFlag = false;
	} else if (ajaxValidOrgCode(path + "/isExistOrgCode.do", id)) {
		showErrorMsg(id, EXIST_ORG_CODE_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证姓名
function validUserName(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, USER_NAME_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证办公电话
function validTel(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, TEL_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证移动电话
function validMobile(id) {
	var isFlag = true;
	var re = /^1\d{10}$/;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, MOBILE_MSG);
		isFlag = false;
	} else if (!re.test($.trim($(id).val())) || $.trim($(id).val()) == "11111111111") {
		showErrorMsg(id, MOBILE_ERROR_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

//验证传真
function validFax(id) {
	if (!validIsNotNull(id)) {
		noShowMsg(id);
	} else {
		showSuccessImg(id);
	}
	return true;
}

//验证邮政编码
function validPostCode(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, POST_CODE_MSG);
		isFlag = false;
	} else if (!validIsNaN(id) || !validNumLength(id, 6)) {
		showErrorMsg(id, POST_CODE_ERROR_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

// 验证单位网站
function validURL(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		noShowMsg(id);
		isFlag = true;
	} else {
		if (validIsNaN(id)) {
			showErrorMsg(id, UNIT_WEBSITE_ERROR_MSG);
			isFlag = false;
		} else {
			showSuccessImg(id);
			isFlag = true;
		}
	}
	return isFlag;
}

//验证验证码
function validCaptcha(id) {
	var isFlag = true;
	if (!validIsNotNull(id)) {
		showErrorMsg(id, CAPTCHA_MSG);
		isFlag = false;
	} else if (!ajaxValidCaptcha(path + "/isRightCaptCha.do", id)) {
		showErrorMsg(id, CAPTCHA_ERROR_MSG);
		isFlag = false;
	} else {
		showSuccessImg(id);
		isFlag = true;
	}
	return isFlag;
}

function focusInfo() {
	$(idArray).each(function(i) {
		$(idArray[i]).focus(function() {
			$(this).attr('title', requiredInfoArray[i]);
			showCueMsg(this, requiredInfoArray[i]);
		});
	});
}

function blurInfo() {
	$(requiredArray[0]).blur(function() {
		validLoginCode($(this));
	});
	$(requiredArray[1]).blur(function() {
		validPassword($(this),requiredArray[0]);
	});
	$(requiredArray[2]).blur(function() {
		validRepeatPwd(requiredArray[1], $(this));
	});
	$(notRequiredArray[0]).blur(function() {
		validDjjg(this);
	});
	$(requiredArray[3]).blur(function() {
		validOrgCode($(this));
	});
	$(requiredArray[4]).blur(function() {
		validUserName($(this));
	});
	$(requiredArray[5]).blur(function() {
		validTel($(this));
	});
	$(requiredArray[6]).blur(function() {
		validMobile($(this));
	});
	$(notRequiredArray[1]).blur(function() {
		validFax($(this));
	});
	$(requiredArray[7]).blur(function() {
		validPostCode($(this));
	});
	$(requiredArray[8]).blur(function() {
		validCaptcha($(this));
	});
	$(notRequiredArray[2]).blur(function() {
		validURL($(this));
	});
}

$(function() {
	mouseoverAndOutClass();
	focusInfo();
	blurInfo();
});

function validAll(path) {
	var isFlag = true;
	if (!validLoginCode(requiredArray[0])) {
		isFlag = false;
	}
	if (!validPassword(requiredArray[1],requiredArray[0])) {
		isFlag = false;
	}
	if (!validRepeatPwd(requiredArray[1], requiredArray[2])) {
		isFlag = false;
	}
	if (!validDjjg(notRequiredArray[0])) {
		isFlag = false;
	}
	if (!validOrgCode(requiredArray[3])) {
		isFlag = false;
	}
	if (!validUserName(requiredArray[4])) {
		isFlag = false;
	}
	if (!validTel(requiredArray[5])) {
		isFlag = false;
	}
	if (!validMobile(requiredArray[6])) {
		isFlag = false;
	}
	validFax(notRequiredArray[1]);
	if (!validPostCode(requiredArray[7])) {
		isFlag = false;
	}
	if (!validURL(notRequiredArray[2])) {
		isFlag = false;
	}
	if (!validCaptcha(requiredArray[8])) {
		isFlag = false;
	}
	return isFlag;
}

document.onkeydown = function(e) {
	var theEvent = window.event || e;
	var code = theEvent.keyCode || theEvent.which;
	if (code == 13) {
		$("#gotoRegister").click();
	}
};